﻿using System;
using System.Windows.Markup;

namespace Bookvar.UI.Localization
{
    /// <summary>
    /// XAML extension for retrieving localized resources.
    /// </summary>
    /// <example>
    /// <code>
    /// {r:Resources name}
    /// </code>
    /// where "r" is XML namespace of this class and name is the name of the localized resources.
    /// </example>
    public class ResourcesExtension : MarkupExtension
    {
        private string resourceName;

        public ResourcesExtension()
            : this(string.Empty)
        {
        }

        public ResourcesExtension(string name)
        {
            Name = name;
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        [ConstructorArgument("name")]
        public string Name
        {
            get { return resourceName; }
            set { resourceName = value; }
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Resources.ResourceManager == null)
            {
                throw new InvalidOperationException("ResourceManager was not found.");
            }
            return Resources.ResourceManager.GetObject(Name, Resources.Culture);
        }
    }
}